<?php
namespace app\admin\controller;
use think\Db;

class Actionstat extends Main
{  
    /**
     * 首页统计
    */
    public function index(){
    	$whereAct = [];
        $whereCou = [];
        $begin = $this->request->param('begin');
        $end = $this->request->param('end');
        if($begin || $end){
            $whereAct[] = search_time('hittime',$begin,$end);
            $whereCou[] = search_time('gettime',$begin,$end);
        }

        $actionId = input('id');
		//活动点击次数
		$actionHits = Db::name('action_hit')
            ->alias('h')
            ->where($whereAct)
            ->where('action_id = '.$actionId)
            ->count();
		//活动点击人数
        $peopleHits = Db::name('action_hit')
            ->alias('h')
            ->where($whereAct)
            ->where('action_id = '.$actionId)
        	->field('count(mobile) as num')
            ->group('mobile')
            ->count();
        //活动领券次数
		$couponGets = Db::name('exchange')
        	->alias('e')
            ->where($whereCou)
            ->where('e.action_id = '.$actionId)
        	->join('coupon c','c.id=e.coupon_id')
        	->field('e.* , count(c.action_id) as num')
            ->count();
        //活动领券人数
		$couponPeoples = Db::name('exchange')
        	->alias('e')
            ->where($whereCou)
            ->where('e.action_id = '.$actionId)
        	->join('coupon c','c.id=e.coupon_id')
        	->group('e.mobile')
            ->count();
        //活动消耗积分总和
		$pointSum = Db::name('exchange')
        	->alias('e')
            ->where($whereCou)
            ->where('e.action_id = '.$actionId)
        	->join('coupon c','c.id=e.coupon_id')
            ->sum("e.point");
		$pointSum = ceil($pointSum*100)/100;
        $this->assign('actionId',$actionId);
        $this->assign('actionHits',$actionHits);
        $this->assign('peopleHits',$peopleHits);
        $this->assign('couponGets',$couponGets);
        $this->assign('couponPeoples',$couponPeoples);
        $this->assign('pointSum',$pointSum);
        return $this->fetch();
    }
	
    
	//进行导出领券记录
    public function downExcelExchange(){
        $actionID = input('actionID');
        $where = [];
        $keyword = $this->request->param('keyword');
        if($keyword){
            $where[] = ['e.username|e.mobile|e.cardHyNo|e.cardLevel','like','%'.$keyword.'%'];
        }
    	$begin = $this->request->param('begin');
        $end = $this->request->param('end');
        if($begin || $end){
            $where[] = search_time('gettime',$begin,$end);
        }
    
        $title = '优惠券领取记录'.date("Ymd");
        $field  = ['id','手机号','姓名','卡号','卡级','券名称','扣减积分','赠券号','兑换时间'];
        
        $record = Db::name('exchange')
        	->alias('e')
        	->join('coupon c','c.id = e.coupon_id')
        	->where($where)
            ->field("e.id,e.mobile,e.userName,e.cardHyNo,e.cardLevel,c.coupon_name,c.point,e.certino,e.gettime")
            ->order('e.id desc')
            ->select();
        dump($record);
        (new Common())->downExcel('券领取记录',$field,$record,$title);
    }
}
